package com.ruoyi.sale.controller;

import java.time.LocalDateTime;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.customer.service.ICustomersService;
import com.ruoyi.products.service.IProductsService;
import com.ruoyi.purchase.domain.Purchase;
import com.ruoyi.purchase.service.IPurchaseService;
import com.ruoyi.suppliers.service.ISuppliersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sale.domain.Sale;
import com.ruoyi.sale.service.ISaleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品销售Controller
 * 
 * @author ruoyi
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/sale/sale")
public class SaleController extends BaseController
{
    @Autowired
    private ISaleService saleService;
    @Autowired
    private IProductsService productsService;
    @Autowired
    private ICustomersService customersService;
    @Autowired
    private IPurchaseService purchaseService;

    /**
     * 查询商品销售列表
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sale sale)
    {
        startPage();
        List<Sale> list = saleService.selectSaleList(sale);
        return getDataTable(list);
    }

    /**
     * 导出商品销售列表
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:export')")
    @Log(title = "商品销售", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sale sale)
    {
        List<Sale> list = saleService.selectSaleList(sale);
        ExcelUtil<Sale> util = new ExcelUtil<Sale>(Sale.class);
        util.exportExcel(response, list, "商品销售数据");
    }

    /**
     * 获取商品销售详细信息
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(saleService.selectSaleById(id));
    }

    private void saleInit(@RequestBody Sale sale) {
        Integer CuId = customersService.selIdByCustomerName(sale.getCustomerName());
        Integer prId =  productsService.selIdByPN(sale.getProductName());
        Long opId = purchaseService.selOperIdByUserName(sale.getUserName());
        sale.setCustomerId(CuId);
        sale.setOperatorId(opId);
        sale.setProductId(prId);
    }
    /**
     * 新增商品销售
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:add')")
    @Log(title = "商品销售", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sale sale)
    {   saleInit(sale);
        productsService.reducePorduct(sale);
        return toAjax(saleService.insertSale(sale));
    }

    /**
     * 修改商品销售
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:edit')")
    @Log(title = "商品销售", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sale sale)
    {
        saleInit(sale);
        Integer reduceQu = sale.getSaleQuantity();
        Integer OrPurchaseQuantity = saleService.selectSaleById(sale.getProductId()).getSaleQuantity();
        int Quantity = reduceQu - OrPurchaseQuantity;
        sale.setSaleQuantity(Quantity);
        productsService.reducePorduct(sale);
        sale.setSaleQuantity(reduceQu);
        return toAjax(saleService.updateSale(sale));
    }

    /**
     * 删除商品销售
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:remove')")
    @Log(title = "商品销售", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(saleService.deleteSaleByIds(ids));
    }
    @GetMapping("/statisOrder")
    public  AjaxResult statisOrder(){
        return success(saleService.statisOrder());
    }


    @GetMapping("/statisTotalPrice")
    public  AjaxResult statisTotalPrice(){

        System.out.println(saleService.statisTotalPrice());
        return success(saleService.statisTotalPrice());
    }

    @GetMapping("/statisTpriASCtime")
    public AjaxResult getSales() {
        List<Integer> sales = saleService.getAllSalesOrderedByTime();
//        System.out.println(sales);
        return success(sales);
    }
    @GetMapping("/statisTotal")
    public AjaxResult getSalesTotal() {
        List<Integer> sales = saleService.getAllTotalByTime();
//        System.out.println(sales);
        return success(sales);
    }
    @GetMapping("/statisAllOrder")
    public AjaxResult getAllSalesOrder() {
        List<Integer> sales = saleService.getAllSalesOrder();
//        System.out.println(sales);
        return success(sales);
    }
}
